#include "common.h"
#include "global_config.h"
#include "eeprom_driver.h"
#include "io_module.h"
#include "adc_module.h"
#include "temp_module.h"
#include "flow_module.h"
#include "water_module.h"
#include "valve_module.h"
#include "pid_module.h"
#include "alarm_module.h"
#include "log_module.h"
#include "modbus_app.h"
#include "hmi_module.h"  // 添加HMI模块头文件

/* 全局资源 */
rt_mq_t g_msg_queue = RT_NULL;
rt_mutex_t g_data_mutex = RT_NULL;
rt_event_t g_system_event = RT_NULL;

/* 线程控制块 */
static rt_thread_t main_thread = RT_NULL;
static rt_thread_t io_thread = RT_NULL;
static rt_thread_t adc_thread = RT_NULL;
static rt_thread_t temp_thread = RT_NULL;
static rt_thread_t flow_thread = RT_NULL;
static rt_thread_t water_thread = RT_NULL;
static rt_thread_t valve_thread = RT_NULL;
static rt_thread_t pid_thread = RT_NULL;
static rt_thread_t alarm_thread = RT_NULL;
static rt_thread_t log_thread = RT_NULL;
static rt_thread_t modbus_thread = RT_NULL;
static rt_thread_t hmi_thread = RT_NULL;  // 添加HMI线程

/* 系统状态 */
static struct {
    rt_bool_t running;
    rt_bool_t initialized;
    uint32_t startup_time;
} system_status = {RT_FALSE, RT_FALSE, 0};

static void system_emergency_stop(void)
{
    /* 紧急停止所有输出 */
    LOG_E("紧急停止触发!\n");
    
    /* 在HMI上显示紧急停止状态 */
    dc_screen_write_string(MAIN_SYSTEM_STATUS, "紧急停止");
    
    /* 停止所有线程 */
    system_status.running = RT_FALSE;
    
    /* 设置所有输出到安全状态 */
    io_set_safe_state();
    valve_stop();
    
    /* LED闪烁指示错误 */
    // 实现错误指示
}

int main(void)
{
    rt_err_t result;
    
    /* 初始化系统滴答计时器 */
    system_status.startup_time = rt_tick_get();
    
    /* 初始化通用资源 */
    result = common_init();
    if (result != RT_OK) {
        rt_kprintf("Failed to initialize common resources\n");
        return -1;
    }
    
    /* 初始化EEPROM驱动 */
    result = eeprom_init();
    if (result != RT_OK) {
        LOG_W("EEPROM initialization failed, using default config\n");
    }
    
    /* 初始化系统配置 */
    result = config_init();
    if (result != RT_OK) {
        LOG_E("Configuration initialization failed\n");
        system_emergency_stop();
        return -1;
    }
    
    /* 初始化硬件模块 */
    result = io_module_init();
    if (result != RT_OK) {
        LOG_E("IO module initialization failed\n");
    }
    
    result = adc_module_init();
    if (result != RT_OK) {
        LOG_E("ADC module initialization failed\n");
    }
    
    result = temp_module_init();
    if (result != RT_OK) {
        LOG_E("Temperature module initialization failed\n");
    }
    
    result = flow_module_init();
    if (result != RT_OK) {
        LOG_E("Flow module initialization failed\n");
    }
    
    result = water_module_init();
    if (result != RT_OK) {
        LOG_E("Water quality module initialization failed\n");
    }
    
    result = valve_module_init();
    if (result != RT_OK) {
        LOG_E("Valve module initialization failed\n");
    }
    
    result = pid_module_init();
    if (result != RT_OK) {
        LOG_E("PID module initialization failed\n");
    }
    
    result = alarm_module_init();
    if (result != RT_OK) {
        LOG_E("Alarm module initialization failed\n");
    }
    
    result = log_module_init();
    if (result != RT_OK) {
        LOG_E("Log module initialization failed\n");
    }
    
    result = modbus_app_init();
    if (result != RT_OK) {
        LOG_E("Modbus module initialization failed\n");
    }
    
    // 添加HMI模块初始化
    result = hmi_module_init();
    if (result != RT_OK) {
        LOG_E("HMI module initialization failed\n");
    }
    
    /* 启动所有线程 */
    if (io_thread) rt_thread_startup(io_thread);
    if (adc_thread) rt_thread_startup(adc_thread);
    if (temp_thread) rt_thread_startup(temp_thread);
    if (flow_thread) rt_thread_startup(flow_thread);
    if (water_thread) rt_thread_startup(water_thread);
    if (valve_thread) rt_thread_startup(valve_thread);
    if (pid_thread) rt_thread_startup(pid_thread);
    if (alarm_thread) rt_thread_startup(alarm_thread);
    if (log_thread) rt_thread_startup(log_thread);
    if (modbus_thread) rt_thread_startup(modbus_thread);
    if (hmi_thread) rt_thread_startup(hmi_thread);  // 启动HMI线程
    
    /* 设置系统状态 */
    system_status.running = RT_TRUE;
    system_status.initialized = RT_TRUE;
    
    /* 广播系统启动消息 */
    broadcast_message(MSG_SYSTEM_STARTUP, RT_NULL, 0);
    
    /* 在HMI上显示启动完成 */
    dc_screen_write_string(MAIN_SYSTEM_STATUS, "系统运行中");
    
    LOG_I("System startup completed successfully\n");
    
    /* 主线程变为空闲任务 */
    while (system_status.running) {
        /* 处理系统消息 */
        message_t msg;
        if (rt_mq_recv(g_msg_queue, &msg, sizeof(msg), RT_WAITING_FOREVER) == RT_EOK) {
            if (msg.dst_module == MODULE_MAIN) {
                /* 处理系统级消息 */
                switch (msg.msg_type) {
                    case MSG_SYSTEM_SHUTDOWN:
                        system_status.running = RT_FALSE;
                        break;
                    case MSG_ALARM_TRIGGER:
                        /* 处理关键报警 */
                        if (msg.data != RT_NULL) {
                            alarm_level_t level = *(alarm_level_t*)msg.data;
                            if (level >= ALARM_LEVEL_CRITICAL) {
                                system_emergency_stop();
                                
                                /* 在HMI上显示关键报警 */
                                dc_screen_write_string(MAIN_SYSTEM_STATUS, "关键报警!");
                                hmi_show_alarm_page();
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
    
    /* 系统关闭程序 */
    LOG_I("System shutdown initiated\n");
    
    /* 在HMI上显示关闭状态 */
    dc_screen_write_string(MAIN_SYSTEM_STATUS, "系统关闭中");
    
    /* 广播关闭消息 */
    broadcast_message(MSG_SYSTEM_SHUTDOWN, RT_NULL, 0);
    
    /* 等待线程退出 */
    rt_thread_mdelay(100);
    
    /* 清理资源 */
    if (g_msg_queue) rt_mq_delete(g_msg_queue);
    if (g_data_mutex) rt_mutex_delete(g_data_mutex);
    if (g_system_event) rt_event_delete(g_system_event);
    
    LOG_I("System shutdown completed\n");
    
    return 0;
}